package com.czr.demo;

import com.czr.entity.Blog;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: czr czr
 * Version: 1.0
 * Create Date Time: 2021/11/27 15:56.
 * Update Date Time:
 *
 * @see
 */
public class Demo {

    @Test
    public void test() {
        //treemap();

        TreeMap<Integer, List<Blog>> newCollect = new TreeMap<>(Comparator.reverseOrder());

        /*collect.forEach((k,v) -> {
            newCollect.put(k,v);
        });*/

        newCollect.put(1,null);
        newCollect.put(2,null);
        newCollect.put(3,null);

        newCollect.forEach((k,v) -> {
            System.out.println(k+"----"+v);
        });

    }
    
    public void treemap() {
        TreeMap<Integer, Integer> simap = new TreeMap<>(Comparator.reverseOrder());
        simap.put(2,2);
        simap.put(1,1);
        simap.put(4,5);
        simap.put(3,3);
        simap.put(16,16);
        simap.put(12,12111);

        simap.forEach((k,v) -> {
            System.out.println(k+"----"+v);
        });


        //for (Map.Entry<Integer, Integer> ss:simap.entrySet()) {
        //    System.out.println(ss.getKey()+"---"+ss.getValue());
        //}


    }
    
    public void groupingBy() {
        Product prod1 = new Product(1L, 1, new BigDecimal("15.5"), "面包", "零食");
        Product prod2 = new Product(2L, 2, new BigDecimal("20"), "饼干", "零食");
        Product prod3 = new Product(3L, 3, new BigDecimal("30"), "月饼", "零食");
        Product prod4 = new Product(4L, 3, new BigDecimal("10"), "青岛啤酒", "啤酒");
        Product prod5 = new Product(5L, 10, new BigDecimal("15"), "百威啤酒", "啤酒");
        List<Product> prodList = Lists.newArrayList(prod1, prod2, prod3, prod4, prod5);
        Map<Object, List<Product>> collectMap = prodList.stream().collect(Collectors.groupingBy(new Function<Product, Object>() {
            @Override
            public Object apply(Product product) {
                return product.getCategory() + "_" + product.getName();
            }
            // item -> item.getCategory() + "_" + item.getName())
        }));

        Set<Map.Entry<Object, List<Product>>> entries = collectMap.entrySet();
        for (Map.Entry<Object, List<Product>> entry : entries) {
            System.out.println(entry.getKey()+"_____"+entry.getValue());
        }
    }

}
